package com.kun.base.shiro.service;

import com.kun.base.model.User;
import com.kun.base.redis.RedisClient;
import com.kun.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by admin on 2017/11/13.
 */
@Service
public class SSOService {

    private static final String ONLINE_KEY="online_user";
    private RedisClient<LinkedHashMap<Integer,String>> redisClient=new RedisClient<LinkedHashMap<Integer, String>>();

    @Autowired
    @Qualifier("jedisConnectionFactory")
    private JedisConnectionFactory jedisConnectionFactory;

    /**
     * 将用户添加到在线列表并做单点登陆处理
     * @param session
     */
    public void addUserToOnline(HttpSession session) {
        //获取用户id
        User user= (User) session.getAttribute("user");
        if (user.getId()==null){
            throw new NullPointerException("登陆异常,用户id为空");
        }

        LinkedHashMap<Integer, String> onlineUser = redisClient.get(ONLINE_KEY);
        if (onlineUser==null){//缓存里没有在线用户则新创建一个map对象用来存储在线用户
            onlineUser=new LinkedHashMap<Integer, String>();
        }else {

            RedisOperationsSessionRepository redisOperationsSessionRepository = SpringUtil.getBean(RedisOperationsSessionRepository.class);

            //如果用户id已经在在线列表里,那么删除用户在线的session
            Iterator<Map.Entry<Integer, String>> iterator = onlineUser.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer, String> next = iterator.next();
                if (next.getKey().intValue()==user.getId().intValue()){
                    //删除原先存在的session
                    redisOperationsSessionRepository.deleteById(next.getValue());
                    //将其从在线列表删除
                    iterator.remove();
                }
            }
        }

        //将新登陆的用户添加到在线列表
        onlineUser.put(user.getId(),session.getId());

        //保存到redis
        redisClient.replace(ONLINE_KEY,onlineUser);
    }


    /**
     * 根据sessionID删除用户在线列表
     * @param session
     */
    public void onlineUserRemove(HttpSession session){
        LinkedHashMap<Integer, String> onlineUser = redisClient.get(ONLINE_KEY);
        if (onlineUser==null){//缓存里没有在线用户则新创建一个map对象用来存储在线用户
            onlineUser=new LinkedHashMap<Integer, String>();
        }else {
            Iterator<Map.Entry<Integer, String>> iterator = onlineUser.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer, String> next = iterator.next();
                if (next.getValue().equals(session.getId())){
                    //将其从在线列表删除
                    iterator.remove();
                }
            }
        }
        //保存到redis
        redisClient.replace(ONLINE_KEY,onlineUser);
    }
}
